<template>
  <div class="IM">
    <header>
      <span>消息</span>
      <van-icon class="clear_icon" :name="$img('clear_msg.png')" size=".44rem" @click="clearList"/>
    </header>
    <notice :top="$remToPx('0.88')"></notice>
    <div class="msg_box">
      <div class="item" v-for="(item, index) in chatList" :key="index" @click="toChat(item)">
        <div class="msg_item">
          <van-image
            width=".88rem"
            height=".88rem"
            fit="cover"
            :radius="$remToPx(0.08)"
            :src="item.latestMessage.content && item.latestMessage.content.user && item.latestMessage.content.user.portrait"
          />
          <div class="other_info">
            <p>{{item.latestMessage.content.user.name}}</p>
            <span class="van-ellipsis">{{item.latestMessage.content.content}}</span>
          </div>
          <div class="time_num">
            <div>{{time(item.sentTime)}}</div>
            <van-tag class="tag_num" round type="warning" color="#FF2A1F" text-color="#FFFFFF" v-if="item.unreadMessageCount > 0">{{item.unreadMessageCount}}</van-tag>
          </div>
        </div>
        <van-divider />
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  data () {
    return {
      chatList: []
    }
  },
  computed: {
    ...mapGetters(['imHis', 'uifo'])
  },
  watch: {
    imHis (val) {
      console.log('更新后的list', val)
      this.chatList = val
    }
  },
  methods: {
    time (time) {
      const otime = new Date(time)
      return `${otime.getMonth() + 1}-${otime.getDate()} ${otime.getHours()}:${otime.getMinutes()}`
    },
    toChat (item) {
      const msData = item.latestMessage.content
      // eslint-disable-next-line no-unused-vars
      let userInfo
      if (msData.user.id !== this.uifo.id) {
        // 判断这条历史纪录最后的发送人是谁
        userInfo = msData.user
      } else {
        userInfo = msData.extra
      }

      const u = {
        nickname: userInfo.name,
        id: userInfo.id,
        avatar: userInfo.portrait ? userInfo.portrait : userInfo.avatar,
        lastTime: item.sentTime
      }

      this.$store.commit('SET_IMUSER', u)

      this.$router.push({
        name: 'chatpage'
      })
    },
    clearList () {
      this.$store.commit('SET_IMHIS', [])
      this.$RC_clearConversations()
    }
  },
  created () {
    this.chatList = this.$store.state.imHis
  }
}
</script>

<style lang="scss">
.IM{
  height: 100%;
  header{
    height: .88rem;
    color: #ffffff;
    font-size: .36rem;
    background: #e0261b;
    padding-left: .32rem;
    display: flex;
    align-items: center;
    .clear_icon{
      margin-left: .1rem;
    }
  }
  .msg_box{
    height: calc(100% - 1.36rem);
    overflow: auto;
  }
  .item{
    padding: 0 .32rem;
    .msg_item{
      padding: .26rem 0 0;
      width: 100%;
      display: flex;
      align-items: center;
      .other_info{
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        margin-left: .12rem;
        margin-right: .46rem;
        &>span{
          max-width: 4.48rem;
          margin-top: .16rem;
          color: #999999;
          font-size: .24rem;
        }
        &>p{
          color: #333333;
          font-size: .28rem;
          font-weight: bold;
        }
      }
      .time_num{
        flex: 0 1 auto;
        display: flex;
        width: auto;
        flex-direction: column;
        &>div{
          color: #999999;
          font-size: .22rem;
          margin-bottom: .2rem;
          white-space:nowrap;
        }
        .tag_num{
          display: flex;
          justify-content: center;
        }
      }
    }
  }
}
</style>
